<?php
namespace App\Admin\Renderable;

use App\Models\Circle\WxCircle;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxPostsVideo;
use App\Models\Posts\WxTag;
use App\Wen\Utils\Settings;
use App\Wen\Utils\Utils;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use Dcat\Admin\Widgets\Metrics\Card;


class PostRender extends LazyRenderable
{
    public function grid(): Grid
    {
        return Grid::make(new WxPost(), function (Grid $grid) {
            global $__MINI_GLOBAL_TENANT_ID__;
            if($__MINI_GLOBAL_TENANT_ID__ > 0){
                $grid->model()->where('tenant_id', '=', $__MINI_GLOBAL_TENANT_ID__);
            }
            $grid->simplePaginate();
            $grid->quickSearch(['id', 'posts_content','user_id','user.user_name', 'circle_name'])->placeholder('搜索文章ID，内容，用户ID，用户名字，圈子名字...')->width(50);

            $grid->model()->orderBy('is_sticky', 'desc');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('is_sticky')->using([0 => '否', 1 => '↑'])->label([
                0 => 'white',
                1 => 'red'
            ])->sortable();
            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('/users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('posts_content')->display('更多') // 设置按钮名称
            ->expand(function ($a) {
                $content = _mini_phone(_mini_aite_replace(_mini_emoji($this->posts_content, true), true), true);
                $images = $this->images_url;
                if($images){
                    $images = Utils::imgWithStyle($images, 0, true);
                }
                $address = $this->address;
                $tagIds = WxPostsTag::where('posts_id', $this->id)->pluck('tags_id');
//                dump($tagIds);
                $tags = WxTag::whereIn('id', $tagIds)->get();
                $video = WxPostsVideo::where('posts_id', $this->id)->first();
                // 只填充内容，不需要标题
                $card = Card::make(view('admin/posts/detail', compact('content', 'images', 'address', 'tags', 'video')));
                return $card;

            })->width('150px');
            $grid->column('pay_content_id')->display(function ($v){
                if(_empty_($v)){
                    return '';
                }
                return '<a target="_blank" href="'.admin_url('posts/paycontent?id='.$v).'">付费内容</a>';
            });

//            $grid->column('images_url')->display(function ($pictures) {
//
//                return $pictures;
//
//            })->image('',200,90);
//            $grid->column('circle_id');

            $grid->column('circle_name');
//            $grid->column('tags')->display(function($v){
//                return $this->id;
//            });
//            $grid->column('address_id');
//            $grid->column('tags_id');
//            $grid->column('is_information')->select([0=>'否',1=>'是']);
            $grid->column('is_information')->switch()->sortable();
            $grid->column('is_examine')->using([0 => '待审核', 1 => '审核通过', 2 => '驳回'])->label([
                0 => 'red',
                1 => 'green',
                2 => 'default'
            ])->sortable();
//            $grid->column('overrule_content');
//            $grid->column('overrule_is_read');
            $grid->column('posts_state')->using([0 => '推送中', 1 => '用户删除', 2 => '已下架'])->label([
                0 => 'primary',
                1 => 'red',
                2 => 'default'
            ])->sortable();
            $grid->column('created_at');



            $grid->disableCreateButton();
            $grid->disableActions();
            // 禁用编辑按钮
            $grid->disableEditButton();
            // 禁用详情按钮
            $grid->disableViewButton();
            $grid->disableBatchActions();
        });
    }
}

